package admin

import org.springframework.dao.DataIntegrityViolationException

class BannerHomeController {

      static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

      def index() {
            redirect(action: "list", params: params)
      }

      def list() {
            params.max = Math.min(params.max ? params.int('max') : 10, 100)
            [bannerHomeInstanceList: BannerHome.list(params), bannerHomeInstanceTotal: BannerHome.count()]
      }

      def create() {
            [bannerHomeInstance: new BannerHome(params)]
      }

      def save() {
            def bannerHomeInstance = new BannerHome(params)
            if (!bannerHomeInstance.save(flush: true)) {
                  render(view: "create", model: [bannerHomeInstance: bannerHomeInstance])
                  return
            }

            flash.message = message(code: 'default.created.message', args: [message(code: 'bannerHome.label', default: 'BannerHome'), bannerHomeInstance.id])
            redirect(action: "show", id: bannerHomeInstance.id)
      }

      def show() {
            def bannerHomeInstance = BannerHome.get(params.id)
            if (!bannerHomeInstance) {
                  flash.message = message(code: 'default.not.found.message', args: [message(code: 'bannerHome.label', default: 'BannerHome'), params.id])
                  redirect(action: "list")
                  return
            }

            [bannerHomeInstance: bannerHomeInstance]
      }

      def edit() {
            def bannerHomeInstance = BannerHome.get(params.id)
            if (!bannerHomeInstance) {
                  flash.message = message(code: 'default.not.found.message', args: [message(code: 'bannerHome.label', default: 'BannerHome'), params.id])
                  redirect(action: "list")
                  return
            }

            [bannerHomeInstance: bannerHomeInstance]
      }

      def update() {
            def bannerHomeInstance = BannerHome.get(params.id)
            if (!bannerHomeInstance) {
                  flash.message = message(code: 'default.not.found.message', args: [message(code: 'bannerHome.label', default: 'BannerHome'), params.id])
                  redirect(action: "list")
                  return
            }

            if (params.version) {
                  def version = params.version.toLong()
                  if (bannerHomeInstance.version > version) {
                        bannerHomeInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                              [message(code: 'bannerHome.label', default: 'BannerHome')] as Object[],
                          "Another user has updated this BannerHome while you were editing")
                        render(view: "edit", model: [bannerHomeInstance: bannerHomeInstance])
                        return
                  }
            }

            if(params.imagem.size > 0){
                  bannerHomeInstance.properties = params
                  def im = "imagem"          
                  def f = request.getFile(im)
                  bannerHomeInstance.imagem = f.getBytes()
            }
            else{
                  bannerHomeInstance.nome = params.nome
            }

            if (!bannerHomeInstance.save(flush: true)) {
                  render(view: "edit", model: [bannerHomeInstance: bannerHomeInstance])
                  return
            }

            flash.message = message(code: 'default.updated.message', args: [message(code: 'bannerHome.label', default: 'BannerHome'), bannerHomeInstance.id])
            redirect(action: "show", id: bannerHomeInstance.id)
      }

      def delete() {
            def bannerHomeInstance = BannerHome.get(params.id)
            if (!bannerHomeInstance) {
                  flash.message = message(code: 'default.not.found.message', args: [message(code: 'bannerHome.label', default: 'BannerHome'), params.id])
                  redirect(action: "list")
                  return
            }

            try {
                  bannerHomeInstance.delete(flush: true)
                  flash.message = message(code: 'default.deleted.message', args: [message(code: 'bannerHome.label', default: 'BannerHome'), params.id])
                  redirect(action: "list")
            }
            catch (DataIntegrityViolationException e) {
                  flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'bannerHome.label', default: 'BannerHome'), params.id])
                  redirect(action: "show", id: params.id)
            }
      }
      def view(){
            def imagem = BannerHome.get( params.id )
            if (imagem) {
                  response.setContentLength(imagem.imagem.size())
                  response.setHeader('filename', imagem.nome)
                  OutputStream out = response.outputStream
                  out.write(imagem.imagem)
                  out.close()
            }
            else {
                  response.sendError(404)
            }   
      }
}
